/*
 * EmailHelper.java
 *
 * Created on July 8, 2007, 10:16 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package helpers;

import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import model.LineItem;
import org.ap.internet.email.Email;

/**
 *
 * @author Alirio
 */
public class EmailHelper {
  //public static String SERVER = "ntfp01ven";
  public static String SERVER = "172.23.20.79";  
  public static String SENDER = "pedidos@dusa.com.ve";
  public static String SENDER2 = "mpalis@dusa.com.ve";
  public static String RECIPIENTS = "pedidos@dusa.com.ve";
  public static String RECIPIENTS2 = "mpalis@dusa.com.ve";
  public static String RECIPIENTS3 = "esaavedra@dusa.com.ve";
  public static String SUBJECT = "PEDIDO CON COMENTARIOS DEL SISTEMA DE PEDIDOS REMOTOS";
  public static String BODY_LINE_1 = "Codigo Vendedor:";
  public static String BODY_LINE_2 = "Codigo Cliente:";
  public static String BODY_LINE_3 = "Numero del Pedido:";
  public static String BODY_LINE_4 = "Comentario:";
  public static String BODY_LINE_5 = "Hora:";
  public static String BODY_LINE_6 = "NOTA: ATENCION! PEDIDO GENERADO POR EL SISTEMA REMOTO DE TOMA DE PEDIDOS CON COMENTARIOS";
  public static String BODY_LINE_7 = "Detalle del Pedido :";
  
  
  
  /** Creates a new instance of EmailHelper */
  public EmailHelper() {

  }
  
  public static void sendCommentEmail(String salesman, String customer, String customerName, long order, String comments, String time,List detalle_item,String mail, String mail_coordinador) {  
    String body = BODY_LINE_1 + salesman + "\n";
    body = body + BODY_LINE_2 + customer + " " + customerName + "\n";
    body = body + BODY_LINE_3 + order + "\n";
    body = body + BODY_LINE_4 + comments + "\n";
    body = body + BODY_LINE_5 + time + "\n";
    body = body + "\n" + BODY_LINE_6 +  "\n";
    body = body + "\n" + BODY_LINE_7 +  "\n";
    

    
    for (Object elem : detalle_item) {
        
     LineItem li = (LineItem) elem;
     NumberFormat formatter = new DecimalFormat("#.###"); 
     String s = formatter.format(li.getSubtotal()); //
     
     body= body + "\n" + li.getProduct().getId()+ "\t" + li.getProduct().getDescription() + "\t" + li.getUnit() + "\t" + li.getQuantity() + "\t" + s + "\n";
     
    }
    
    
    
     Email email = new Email(SERVER, SENDER, RECIPIENTS, SUBJECT, body);
     email.send();
    
     if (mail.trim().compareTo("")!=0)
     {
         email = new Email(SERVER, SENDER, mail, SUBJECT, body);
         email.send();
     }
    
       if (mail_coordinador.trim().compareTo("")!=0)
     {
     email = new Email(SERVER, SENDER, mail_coordinador, SUBJECT, body);
     email.send();
       }
    
   // System.out.println(body);
    
  }
  
  public static void sendDefaultCommentEmail(String salesman, String customer, String customerName, long order, String comments, String time,List detalle_item,String mail, String mail_coordinador) {
    String body = BODY_LINE_1 + salesman + "\n";
    body = body + BODY_LINE_2 + customer + " " + customerName + "\n";
    body = body + BODY_LINE_3 + order + "\n";
    body = body + BODY_LINE_4 + comments + "\n";
    body = body + BODY_LINE_5 + time + "\n";
    body = body + "\n" + BODY_LINE_6 +  "\n";
    
     body = body + "\n" + BODY_LINE_7 +  "\n";
    
    for (Object elem : detalle_item) {
     LineItem li = (LineItem) elem;
     
     NumberFormat formatter = new DecimalFormat("#.###"); 
     String s = formatter.format(li.getSubtotal()); //
     
     body= body + "\n" + li.getProduct().getId()+ "\t" + li.getProduct().getDescription() + "\t" + li.getUnit() + "\t" + li.getQuantity() + "\t" + s + "\n";
     
    }
    //System.out.println(body);
    Email email = new Email(SERVER, SENDER2, RECIPIENTS2, SUBJECT, body);
    email.send();
    
    email = new Email(SERVER, SENDER2, RECIPIENTS3, SUBJECT, body);
    email.send();
    
    if (mail.trim().compareTo("")!=0)
     {
     email = new Email(SERVER, SENDER, mail, SUBJECT, body);
     email.send();
    }
    
    if (mail_coordinador.trim().compareTo("")!=0)
     {
     email = new Email(SERVER, SENDER, mail_coordinador, SUBJECT, body);
     email.send();
    }
  }
}
